package com.gitee.wsl.doc.sample.opml

import com.gitee.wsl.doc.xml.EventType
import com.gitee.wsl.doc.xml.XmlPullParser
import com.gitee.wsl.doc.xml.XmlPullParserException
import com.gitee.wsl.text.format.format

object ValidityCheck {
    @Throws(_root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException::class)
    fun require(xpp: XmlPullParser, position: EventType, name: String) {
        requirePosition(xpp, position)
        requireName(xpp, name)
    }

    @Throws(_root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException::class)
    fun requirePosition(xpp: XmlPullParser, position: EventType) {
        try {
            if (xpp.eventType != position) {
                throw _root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException(
                    String.format(
                        "required position %s but found position %s",
                        translate(position),
                        translate(xpp.eventType)
                    )
                )
            }
        } catch (e: XmlPullParserException) {
            throw _root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException(e)
        }
    }

    @Throws(_root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException::class)
    fun requireName(xpp: XmlPullParser, name: String) {
        if (xpp.name != name) {
            throw _root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException(
                String.format(
                    "required element <%s> but found <%s>",
                    name,
                    xpp.name
                )
            )
        }
    }

    @Throws(_root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException::class)
    fun requireNoText(xpp: XmlPullParser, elementName: String?, insideElement: Boolean) {
        if (!isTextBlank(xpp)) {
            if (insideElement) {
                throw _root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException(
                    String.format(
                        "text inside element <%s>: \"%s\"",
                        elementName,
                        xpp.text
                    )
                )
            }
            throw _root_ide_package_.com.gitee.wsl.doc.sample.opml.OpmlParseException(
                String.format(
                    "required element <%s> but found text: \"%s\"",
                    elementName,
                    xpp.text
                )
            )
        }
    }

    fun isTextBlank(xpp: XmlPullParser): Boolean {
        return isTextBlank(xpp.text)
    }

    fun isTextBlank(str: String?): Boolean {
        // adapted from Apache commons-lang StringUtils#isBlank(String)
        // licensed under Apache Software License 2.0
        var strLen: Int = 0
        if (str == null || (str.length.also { strLen = it }) == 0) {
            return true
        }
        for (i in 0 until strLen) {
            if (!str[i].isWhitespace()) {
                return false
            }
        }
        return true
    }

    fun translate(position: EventType): String {
        return when (position) {
            EventType.START_DOCUMENT ->  "START_DOCUMENT"
            EventType.START_TAG ->  "START_TAG"
            EventType.TEXT ->  "TEXT"
            EventType.END_TAG ->  "END_TAG"
            EventType.END_DOCUMENT ->  "END_DOCUMENT"
            else -> position.toString()
        }
        //return position.toString()
    }
}
